package interfaceGrafica;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import logica.locacao.PlanoDeLocacao;
import logica.locacao.PlanoDeLocacao.TipoDeVeiculo;
import logica.locacao.Seguro;
import logica.main.MainGrafico;
import logica.util.TipoDeCadastro;
import logica.util.TipoDeEntradaInvalida;
import logica.veiculo.Acessorio;
import persistencia.GeradorDeLogs;
import persistencia.MecanismoDePersistenciaSeguro;
import util.Constantes;

@SuppressWarnings("serial")
public class InterfacePlanoDeLocacao extends javax.swing.JPanel {


	
	private MecanismoDePersistenciaSeguro persistencia;
    private GeradorDeLogs geradorDeLog;
    private PlanoDeLocacao planoAtual;
    private ArrayList<PlanoDeLocacao> planos;
    
    /** Creates new form InterfacePlanoDeLocacao */
    @SuppressWarnings("deprecation")
	public InterfacePlanoDeLocacao() {

        lerDados();

        initComponents();
       
        try{
            geradorDeLog = new GeradorDeLogs();
        }catch(Exception e ){

            JOptionPane.showMessageDialog(null, "Erro com o gerador de logs.", "Erro", JOptionPane.ERROR_MESSAGE);
        }
        try {
			geradorDeLog.escreveLog("Usuario "+MainGrafico.getUsuarioLogado().getLogin()+" entrou na area de gereciamento de Planos de Locacao");
		} catch (Exception e) {
			
		}
        list1.setMultipleSelections(true);
        
        for(PlanoDeLocacao plano : planos){
            list3.add("Cod: "+plano.getCodigo() + " # " + "R$ " + plano.getPrecoBaseDaDiaria() + " # " +
                        "Seguro: "+plano.getSeguro()+" # "+"Tipo do veiculo: "+plano.getTipoDeVeiculo());
        }

        jComboBox1.addItem(TipoDeVeiculo.AUTOMOVEL);
        jComboBox1.addItem(TipoDeVeiculo.MOTOCICLETA);

        for(Seguro cobertura : Seguro.values()){
            jComboBox2.addItem(cobertura);
        }
        
        jButton5.setEnabled(false);
        jButton6.setEnabled(true);
        jButton7.setEnabled(false);        
    }


    private void cadastro(String mensagemSucesso, TipoDeCadastro tipo, String tituloSucesso, String mensagemErro, String tituloErro) throws Exception{

        PlanoDeLocacao plano;

		plano = new PlanoDeLocacao();

		List<TipoDeEntradaInvalida> entradasInvalidas = new ArrayList<TipoDeEntradaInvalida>();

		try {
			if (jComboBox1.getSelectedItem().equals(TipoDeVeiculo.AUTOMOVEL)) {
				plano.setTipoDeVeiculo(TipoDeVeiculo.AUTOMOVEL);
			} else {
				plano.setTipoDeVeiculo(TipoDeVeiculo.MOTOCICLETA);
			}

		} catch (Exception e) {
			entradasInvalidas.add(TipoDeEntradaInvalida.TIPO_DE_VEICULO);
		}

		try {
			plano.setPotencia(Integer.parseInt(jTextField3.getText()));
		} catch (Exception e) {
			entradasInvalidas.add(TipoDeEntradaInvalida.POTENCIA);
		}

		try {
			plano.setPrecoBaseDaDiaria(Double.valueOf(jTextField4.getText()));
		} catch (Exception e) {
			entradasInvalidas.add(TipoDeEntradaInvalida.PRECO_BASE);
		}

		try {

			Acessorio[] acessorios = new Acessorio[list1.getSelectedItems().length];

			for (int i = 0; i < list1.getSelectedItems().length; i++) {
				for (Acessorio a : Acessorio.values()) {
					if (a.getDescricao().equals(list1.getSelectedItems()[i])) {
						acessorios[i] = a;
					}
				}
			}

			plano.setAcessorios(acessorios);

		} catch (Exception e) {
			entradasInvalidas.add(TipoDeEntradaInvalida.ACESSORIOS);
		}

		try {
			if (jComboBox2.getSelectedItem().equals(Seguro.PARCIAL)) {
				plano.setSeguro(Seguro.PARCIAL);
			} else if ( jComboBox2.getSelectedItem().equals(Seguro.TOTAL)) {
				plano.setSeguro(Seguro.TOTAL);
			}
			else{
				plano.setSeguro(Seguro.SEM_SEGURO);
			}

		} catch (Exception e) {

			entradasInvalidas.add(TipoDeEntradaInvalida.SEGURO);
		}

		try {

			HashMap<String, HashSet<String>> mapaMarca = new HashMap<String, HashSet<String>>();

			TableModel tabelaModelo = jTable1.getModel();

			HashSet<String> carros;

			String carro = "";

			for (int l = 0; l < tabelaModelo.getRowCount(); l++) {

				carros = new HashSet<String>();

				for (int c = 1; c < tabelaModelo.getColumnCount(); c++) {

					carro = String.valueOf(tabelaModelo.getValueAt(l, c));

					if ((!(carro).equals("null")) && (!(carro).equals(""))) {
						carros.add(String
								.valueOf(tabelaModelo.getValueAt(l, c)));
					}

				}

				if ((!tabelaModelo.getValueAt(l, 0).equals("null"))
						&& (!tabelaModelo.getValueAt(l, 0).equals(""))) {

					mapaMarca.put(
							String.valueOf(tabelaModelo.getValueAt(l, 0)),
							carros);
				}

			}
			plano.setMarcasXModelos(mapaMarca);

		} catch (Exception e) {
			entradasInvalidas.add(TipoDeEntradaInvalida.MARCA);
		}

		if (entradasInvalidas.size() == 0) {

			planos.add(plano);

			list3.add("Cod: " + plano.getCodigo() + " # " + "R$ "
					+ plano.getPrecoBaseDaDiaria() + " # " + "Seguro: "
					+ plano.getSeguro() + " # " + "Tipo do veiculo: "
					+ plano.getTipoDeVeiculo());

			JOptionPane.showMessageDialog(null, mensagemSucesso, tituloSucesso,
					JOptionPane.INFORMATION_MESSAGE);
			gravaDados();
			limpaDados();
			list1.select(-1);
	        
		} else {
			if (tipo == TipoDeCadastro.EDICAO) {
				planos.add(plano);
				try {
					geradorDeLog.escreveLog("O plano de locacao "+plano.getCodigo()+" teve suas informacoes alteradas.");
				} catch (Exception e) {
				}
				list3.add("Cod: " + plano.getCodigo() + " # " + "R$ "
						+ plano.getPrecoBaseDaDiaria() + " # " + "Seguro: "
						+ plano.getSeguro() + " # " + "Tipo do veiculo: "
						+ plano.getTipoDeVeiculo());
				list1.select(list1.getItemCount() - 1);
		        jButton5.setEnabled(true);
		        jButton6.setEnabled(false);
		        jButton7.setEnabled(true);
			}
			JOptionPane.showMessageDialog(null, mensagemErro, tituloErro,
					JOptionPane.ERROR_MESSAGE);
			for (TipoDeEntradaInvalida entradaInvalida : entradasInvalidas) {
				if (entradaInvalida == TipoDeEntradaInvalida.TIPO_DE_VEICULO) {
					jLabel6.setForeground(Color.red);
				} else if (entradaInvalida == TipoDeEntradaInvalida.SEGURO) {
					jLabel8.setForeground(Color.red);
				} else if (entradaInvalida == TipoDeEntradaInvalida.CILINDRADAS) {
					jLabel9.setForeground(Color.red);
				} else if (entradaInvalida == TipoDeEntradaInvalida.POTENCIA) {
					jLabel10.setForeground(Color.RED);
				} else if (entradaInvalida == TipoDeEntradaInvalida.MARCA) {
					jTable1.setGridColor(Color.red);
					jLabel11.setForeground(Color.RED);
				} else if (entradaInvalida == TipoDeEntradaInvalida.PRECO_BASE) {
					jLabel1.setForeground(Color.RED);
				}
			}
		}
	}

    @SuppressWarnings("unchecked")
	private void lerDados(){
        try{

            persistencia = new MecanismoDePersistenciaSeguro("arquivos/planodelocacao.ser");
            planos = (ArrayList<PlanoDeLocacao>) persistencia.recuperaDoArquivo();
            if(planos == null){
            	planos = new ArrayList<PlanoDeLocacao>();
            }

        }catch(Exception e){
        }    
    }


  
    private void seleciona(){

       DefaultTableModel modeloTabela = (DefaultTableModel) jTable1.getModel();;

       String[] valores = list3.getSelectedItem().split(" # ");
       String codigo = valores[0].split(" ")[1];

     for(int i = 0; i < list1.getItems().length; i++){
        list1.deselect(i);
     }

       for(PlanoDeLocacao plano : planos){
            if(plano.getCodigo() == Integer.parseInt(codigo)){

                jComboBox1.setSelectedItem(plano.getTipoDeVeiculo());
                jComboBox2.setSelectedItem(plano.getSeguro());

                jTextField4.setText(String.valueOf(plano.getPrecoBaseDaDiaria()));
                jTextField2.setText(String.valueOf(plano.getCilindradas()));
                jTextField3.setText(String.valueOf(plano.getPotencia()));
                jTextField5.setText(String.valueOf(plano.getCodigo()));

                for(Acessorio acessorio : plano.getAcessorios()){
                    for(int i = 0; i < list1.getItems().length; i++){
                        if(list1.getItem(i).equals(acessorio.getDescricao())){
                            list1.select(i);
                        }
                    }
                }


                while(modeloTabela.getRowCount() != plano.getMarcaXModelos().keySet().size()){
                    if(modeloTabela.getRowCount() > plano.getMarcaXModelos().keySet().size()){
                        modeloTabela.removeRow(modeloTabela.getRowCount() - 1);
                    }
                    else if(modeloTabela.getRowCount() < plano.getMarcaXModelos().keySet().size()){
                        modeloTabela.addRow(new String[10]);
                    }

                }

                int linha = 0;
                int coluna = 1;

                for(String chave : plano.getMarcaXModelos().keySet()){

                    coluna = 1;

                    
                     modeloTabela.setValueAt(chave, linha, 0);


                     for(String valor : plano.getMarcaXModelos().get(chave)){

                         modeloTabela.setValueAt(valor, linha, coluna);
                         coluna++;

                      }

                     linha++;
                }

                jTable1.setModel(modeloTabela);

            }

            planoAtual = plano;
       }

    }

    @SuppressWarnings("unchecked")
	private void gravaDados(){

        try{

            persistencia.persisteNoArquivo((ArrayList) planos);
            geradorDeLog.escreveLog("Plano de locacao salvos com sucesso");

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro na gravacao: "+e.getMessage(), "Erro na gravacao", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void limpaDados(){


        //Deseleciona todos os acessorios
        for( int i=0; i < list1.getItemCount(); i++){
            list1.deselect(i);
        }

        //Deseleciona todos os texts fields
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");

        jLabel1.setForeground(Color.BLACK);
        jLabel2.setForeground(Color.BLACK);
        jLabel4.setForeground(Color.BLACK);
        jLabel5.setForeground(Color.BLACK);
        jLabel6.setForeground(Color.BLACK);
        jLabel7.setForeground(Color.BLACK);
        jLabel8.setForeground(Color.BLACK);
        jLabel9.setForeground(Color.BLACK);
        jLabel10.setForeground(Color.BLACK);
        jLabel11.setForeground(Color.BLACK);
       
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();

        modelo.setNumRows(1);
        for(int i = 0; i < modelo.getColumnCount(); i++){
            modelo.setValueAt("", 0, i);
        }

    }


   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane1 = new java.awt.ScrollPane();
        list3 = new java.awt.List();
        jLabel4 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        scrollPane3 = new java.awt.ScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        scrollPane2 = new java.awt.ScrollPane();
        list1 = new java.awt.List();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton8 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setToolTipText("Gerenciamento de Plano");

        list3.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                list3ItemStateChanged(evt);
            }
        });
        scrollPane1.add(list3);

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 15));
        jLabel4.setText("Plano de locacao");

        jLabel11.setText("Marcas x Modelos:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Marca", "Modelo 1", "Modelo 2", "Modelo 3", "Modelo 4", "Modelo 5", "Modelo 6", "Modelo 7", "Modelo 8", "Modelo 9", "Modelo 10"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        scrollPane3.add(jScrollPane1);

        jButton3.setText("--");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setText("+");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel5.setText("Consultar:");

        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox1MouseClicked(evt);
            }
        });
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel6.setText("Tipo do Veiculo:");

        scrollPane2.add(list1);

        jLabel7.setText("Acessorios:");

        jLabel9.setText("Cilindrada:");

        jLabel8.setText("Seguro:");

        jLabel10.setText("Potencia:");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton8.setText("Voltar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton7.setText("Salvar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton4.setText("Limpar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton6.setText("Cadastrar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton5.setText("Excluir");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel1.setText("Valor base do plano:");

        jLabel2.setText("Cod:");

        jTextField5.setEditable(false);

        jLabel3.setText("R$ :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                                        .addComponent(jLabel2))
                                    .addGap(12, 12, 12)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(12, 12, 12)
                                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel6))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel8)
                                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(24, 24, 24))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                                .addComponent(jLabel9))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel10)
                                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGap(19, 19, 19))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel3)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(scrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3))
                            .addComponent(scrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addComponent(scrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton6)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(scrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(scrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(scrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton8)
                    .addComponent(jButton7)
                    .addComponent(jButton6)
                    .addComponent(jButton5)
                    .addComponent(jButton4))
                .addContainerGap(24, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

        try{
          
          planos.remove(planoAtual);
          list3.remove("Cod: "+planoAtual.getCodigo() + " # " + "R$ " + planoAtual.getPrecoBaseDaDiaria() + " # " +
                        "Seguro: "+planoAtual.getSeguro()+" # "+"Tipo do veiculo: "+planoAtual.getTipoDeVeiculo());
        
          cadastro("Edicao realizada com sucesso", TipoDeCadastro.EDICAO, "Edicao de plano", "A edicao falhou. Os campos errados estao em destaque.", "Edicao de plano");

          gravaDados();

        }catch(Exception e){

        }


      
}//GEN-LAST:event_jButton7ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

    limpaDados();
    list3.select(-1);
    jButton5.setEnabled(false);
    jButton6.setEnabled(true);
    jButton7.setEnabled(false);    
        

}//GEN-LAST:event_jButton4ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        try {
            cadastro("Cadastro realizado com sucesso", TipoDeCadastro.NOVA_ENTRADA, "Cadastro de plano", "O cadastro falhou. Os campos errados estao em destaque.", "Cadastro de plano");
            list3.select(-1);
            jButton5.setEnabled(false);
            jButton6.setEnabled(true);
            jButton7.setEnabled(false);            
        } catch (Exception ex) {
        }


    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

        if(planoAtual != null){
            
            planos.remove(planoAtual);
            try {
				geradorDeLog.escreveLog("O plano de locacao "+planoAtual.getCodigo()+" teve seus registros excluidos do sistema.");
			} catch (Exception e) {
			}
            list3.remove("Cod: "+planoAtual.getCodigo() + " # " + "R$ " + planoAtual.getPrecoBaseDaDiaria() + " # " +
                        "Seguro: "+planoAtual.getSeguro()+" # "+"Tipo do veiculo: "+planoAtual.getTipoDeVeiculo());
            planoAtual = null;
            JOptionPane.showMessageDialog(null, "Plano excluido com sucesso!", "Plano removido", JOptionPane.PLAIN_MESSAGE);
            limpaDados();
            gravaDados();
            list3.select(-1);
            jButton5.setEnabled(false);
            jButton6.setEnabled(true);
            jButton7.setEnabled(false);            
        }

}//GEN-LAST:event_jButton5ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged

        list1.removeAll();
        jTextField2.setText("0");
        jTextField2.setEnabled(false);

        if(jComboBox1.getSelectedItem().equals(TipoDeVeiculo.AUTOMOVEL)){
            
            for( Acessorio acessorio : Acessorio.values() ){
                if(acessorio.getPresenca() <= 0 ){
                    list1.add(acessorio.getDescricao());
                }
            }

            
            jTextField2.setText("");

        }else{
            for( Acessorio acessorio : Acessorio.values() ){
                if(acessorio.getPresenca() >= 0 ){
                    list1.add(acessorio.getDescricao());
                }
            }

            jTextField2.setEnabled(true);
        }
        
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox1MouseClicked

      
    }//GEN-LAST:event_jComboBox1MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
       modelo.addRow(new String[10]);
       jTable1.setModel(modelo);
      
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        String marca;
        
        marca = (String) modelo.getValueAt((modelo.getRowCount() - 1), 0);

        if(marca != null){
            planoAtual.getMarcaXModelos().remove(marca);
        }


        if(modelo.getRowCount() > 1){
            modelo.removeRow(modelo.getRowCount() - 1);
            jTable1.setModel(modelo);
        }
    }//GEN-LAST:event_jButton3ActionPerformed


    private void list3ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_list3ItemStateChanged

        seleciona();
        jButton5.setEnabled(true);
        jButton6.setEnabled(false);
        jButton7.setEnabled(true); 
        
    }//GEN-LAST:event_list3ItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String parametroDeConsulta = jTextField1.getText();

        String[] consultados = list3.getItems();

        for ( int i=0; i<consultados.length; i++ ){
            if ( (consultados[i].toLowerCase()).
                    contains(parametroDeConsulta.toLowerCase()) ){
                list3.select(i);
                jTextField1.setText("");
                seleciona();
                break;
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        MainGrafico.entrarNaArea(MainGrafico.getMenuCorrente(), Constantes.INTERFACE_TAMANHO_MENU[0], Constantes.INTERFACE_TAMANHO_MENU[1]);
        MainGrafico.sairDaArea(this);
    }//GEN-LAST:event_jButton8ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private java.awt.List list1;
    private java.awt.List list3;
    private java.awt.ScrollPane scrollPane1;
    private java.awt.ScrollPane scrollPane2;
    private java.awt.ScrollPane scrollPane3;
    // End of variables declaration//GEN-END:variables

}
